<?php
namespace Weibo\Controller;

use Weibo\Controller\WeibobaseController;

/**
 * 微博认证
 * @author    Baiyu    2016-12-24
 */
class VerifyController extends WeibobaseController {
	
	/**
	 * 提交认证申请
	 * @author    Zhangwei    2016-12-24
	 */
	public function add() {
		if (IS_POST) {
			$weibo_verify = D('WeiboVerifyApply');
			$apply_id	  = I('post.apply_id');
			if ($apply_id) {
				$apply_info = $weibo_verify->field('multiclass_photo,handheld_photo,identity_photo,credential4')->find($apply_id);
			}
			//图片上传方法
			$upload = new \Think\Upload();
			$upload -> maxSize  = 3145728;
			$upload -> exts 	= array('jpg', 'png', 'jpeg', 'gif');
			$upload -> rootPath = './data/upload/';
			$upload -> savePath = 'weiboverify/';
			$info = $upload -> upload();
				if ( $weibo_verify -> create() ) {
					$weibo_verify -> apply_time = time();
					$weibo_verify -> user_id	= $this ->userId;
					if (!$apply_id) {
						$result = $weibo_verify->add();
					} else {
						if($info){
							// 判断那个字段重新上传了图片，然后删除对应的原图片 ;
							foreach($info as $key =>$value){
								foreach($apply_info as $key2 => $value2){
									if($key2 == $key){
										$weibo_verify->$key2 = $info[$key2]['savepath'] . $info[$key2][savename];
										$simg = './' . C('UPLOADPATH') . $apply_info[$key2];
										@unlink($simg);
									}
								}
							}
						}
						$weibo_verify -> refuse_reason = '';
						$weibo_verify -> approved	   = 0;  //认证状态重新变为审核中状态
						$result = $weibo_verify -> where('apply_id=' . $apply_id)->save();
					}
					if ($result) {
						$assign['msg']	  = 'success';
						$assign['status'] = 1;
					} else {
						$assign['error'] 	 = 101;
						$assign['error_msg'] = $weibo_verify ->getError();
					}
				}else{
					$assign['error']	 = 101;
					$assign['error_msg'] = $weibo_verify ->getError();
				}
				$this ->output($assign);
			}
		}
	/**
	 * 查看申请进度
	 * @author    Zhangwei    2016-12-24
	 */
	public function status() {
		if (IS_POST) {
			$user_id = $this ->userId;
			$weibo_verify = D('WeiboVerifyApply');
//			$weibo_verify->cache(true); //关闭缓存功能
			$result = $weibo_verify -> where('user_id=' . $user_id) -> find();
			if ($result) {
				if ($result['approved'] == 1) {
					$assign['msg']	  = '审核成功';
					$assign['status'] = 1;
				} else if($result['approved'] == 2) {
					$result['refuse_reason'] = json_decode($result['refuse_reason'],true);
					$result['multiclass_photo'] = sp_get_image_preview_url($result['multiclass_photo']);
					$result['handheld_photo']	= sp_get_image_preview_url($result['handheld_photo']);
					$result['identity_photo']	= sp_get_image_preview_url($result['identity_photo']);
					$assign['msg']	  = '审核失败';
					$assign['status'] = 2;
					$assign['info']   = $result;
				}else{
					$assign['msg']	  = '审核中';
					$assign['status'] = 0;
				}
			} else {
				$assign['error'] = 1001;
				$assign['error_msg'] = '没有认证信息';
			}
			$this->output($assign);
		}
	}
	
}